/**
 * Created by Zhongyi on 5/2/16.
 */

'use strict';

import {shell} from "electron";

const path = require('path');
const { app, Menu, nativeImage, Tray, ipcMain } = require('electron');

const AppConfig = require('../../configuration');

const assetsPath = path.join(__dirname, '../../../assets');

let Common = require('../../common_cn');
// 是否可以退出
let trayClose = false

class AppTray {

    // constructor(splashWindow, mainWindow) {
    constructor( mainWindow) {
        // this.splashWindow = splashWindow;
        this.mainWindow = mainWindow;
        this.lastUnreadStat = 0;
        const trayColor = AppConfig.readSettings('tray-color');
        if (trayColor === 'white' || trayColor === 'black') {
            this.trayColor = trayColor;
        } else {
            this.trayColor = 'white';
            AppConfig.saveSettings('tray-color', this.trayColor);
        }
        this.createTray();
    }

    createTray() {
        let image;
        if (process.platform === 'linux' || process.platform === 'win32') {
            image = nativeImage.createFromPath(path.join(assetsPath, `tray_${this.trayColor}.png`));
            this.trayIcon = image;
            this.trayIconUnread = nativeImage.createFromPath(path.join(assetsPath, `tray_unread_${this.trayColor}.png`));
        } else {
            image = nativeImage.createFromPath(path.join(assetsPath, 'status_bar.png'));
        }
        image.setTemplateImage(true);

        this.tray = new Tray(image);


        ipcMain.on('refreshIcon', () => this.refreshIcon());

        if (process.platform === 'linux' || process.platform === 'win32') {
            // 系统托盘右键菜单
            const trayMenuTemplate = [
                {
                  label: '打开主界面',
                  click: function () {
                      this.hideSplashAndShowWeChat()
                  }
                },
                {
                    label: '关于我们',
                    click: function () {
                        // 打开外部链接
                        shell.openExternal('http://www.baidu.com')
                    }
                },
                {
                    label: '退出',
                    click: function () {
                        // 退出
                        trayClose = true
                        app.quit()
                    }
                }
            ]
            // 图标的上下文
            const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
            this.tray.setContextMenu(contextMenu);
        }
        // 设置此托盘图标的悬停提示内容
        this.tray.setToolTip('天天好心情')
        // this.tray.setToolTip(Common.ELECTRONIC_WECHAT);
        this.tray.on('click', () => this.hideSplashAndShowWeChat());
    }

    setTitle(title) {
        this.tray.setTitle(title);
    }

    hideSplashAndShowWeChat() {
        // if (this.splashWindow.isShown) return;
        // this.mainWindow.show();
        this.mainWindow.isVisible() ? this.mainWindow.hide() : this.mainWindow.show()
    }

    refreshIcon() {
        this.trayColor = AppConfig.readSettings('tray-color');
        this.trayIcon = nativeImage.createFromPath(path.join(assetsPath, `tray_${this.trayColor}.png`));
        this.trayIconUnread = nativeImage.createFromPath(path.join(assetsPath, `tray_unread_${this.trayColor}.png`));
        if (this.lastUnreadStat === 0) {
            this.tray.setImage(this.trayIcon);
        } else {
            this.tray.setImage(this.trayIconUnread);
        }
    }

    setUnreadStat(stat) {
        if (stat === this.lastUnreadStat) return;
        this.lastUnreadStat = stat;
        if (stat === 0) {
            this.tray.setImage(this.trayIcon);
        } else {
            this.tray.setImage(this.trayIconUnread);
        }
    }
}

module.exports = AppTray;
